import os.path
import time

from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from base.base_page import BasePage


class Common(BasePage):
    # 完成
    def btn_complete(self):
        return self.by_id(self.HW_ENV + "btn_complete").click()

    # 文档名
    def btn_filename(self):
        return self.by_id(self.HW_ENV + "left_title").click()

    def btn_copy(self):
        return self.by_uiautomator('new UiSelector().text("复制")').click()

    def btn_to_word(self):
        return self.by_uiautomator('new UiSelector().text("转word")').click()

    def btn_land(self):
        return self.by_uiautomator('new UiSelector().text("横屏查看")').click()

    def btn_filter(self):
        return self.by_uiautomator('new UiSelector().text("过滤")').click()

    def btn_all_select(self):
        return self.by_uiautomator('new UiSelector().text("全选")').click()

    # 导出
    def btn_export(self):
        return self.by_uiautomator('new UiSelector().text("导出")').click()

    def btn_wechat(self):
        return self.by_uiautomator('new UiSelector().text("微信")').click()

    def btn_local(self):
        return self.by_id(self.HW_ENV + 'hor_iv1').click()

    def btn_wechat_helper(self):
        return self.by_uiautomator('new UiSelector().text("文件传输助手")')

    # 微信-文件传输助手-分享
    def btn_wechat_share(self):
        return self.by_uiautomator('new UiSelector().text("分享")').click()

    def btn_wechat_sent(self):
        return self.by_uiautomator('new UiSelector().text("发送")').click()

    # 微信-返回第三方工具
    def btn_wechat_back(self):
        return self.by_uiautomator('new UiSelector().text("返回第三方工具")').click()

    def btn_back(self):
        return self.by_id(self.HW_ENV + "navibar_back").click()

    def btn_img_page(self):
        return self.by_uiautomator('new UiSelector().text("图像")').click()

    def btn_img(self):
        return self.by_uiautomator('new UiSelector().text("图片")').click()

    def btn_long_img(self):
        return self.by_uiautomator('new UiSelector().text("长图")').click()

    def btn_sure(self):
        return self.by_uiautomator('new UiSelector().text("确定")').click()

    # 文档重命名弹窗输入框
    def input_filename(self):
        return self.by_id(self.HW_ENV + "newfolder_edit")

    # 输入框清除
    def btn_clear_input(self):
        return self.by_id(self.HW_ENV + "folder_cross").click()

    # 获取当前屏幕的截图
    def func_take_screenshot(self, name):
        day = time.strftime("%Y-%m-%d", time.localtime(time.time()))
        path = ".\\screenshot\\" + day
        tm = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime(time.time()))
        format = '.png'
        if os.path.exists(path):
            filename = path + "\\" + tm + "_" + name + format
        else:
            os.makedirs(path)
            filename = path + "\\" + tm + "_" + name + format
        self.driver.get_screenshot_as_file(filename)

    def func_edit_filename(self, name):
        tm = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()))
        filename = name + '-' + tm
        WebDriverWait(self.driver, 200).until(EC.element_to_be_clickable((By.ID, self.HW_ENV + 'left_title')))
        self.btn_filename()
        self.btn_clear_input()
        time.sleep(1)
        self.input_filename().send_keys(filename)
        time.sleep(1)
        self.btn_sure()

    def func_export_to_wechat(self):
        WebDriverWait(self.driver, 200).until(EC.presence_of_element_located((By.ID, self.HW_ENV + 'hor_iv2')))
        self.btn_wechat()
        try:
            self.driver.implicitly_wait(2)
            self.btn_sure()
        except NoSuchElementException:
            pass
        WebDriverWait(self.driver, 100).until(EC.presence_of_element_located((By.ID, 'com.tencent.mm:id/g_l')))
        # 有文件传输助手则点击，否则搜索文件传输助手
        if self.btn_wechat_helper():
            self.btn_wechat_helper().click()
        else:
            # 点击搜索
            self.by_id('com.tencent.mm:id/g_l').click()
            # 搜索输入框输入文件传输助手
            self.by_id('com.tencent.mm:id/d98').send_keys('文件传输助手')
            time.sleep(1)
            self.btn_wechat_helper().click()
        # 微信分享按钮由于id总是变化，无法通过id定位：分享文件时为分享，分享图片时为发送
        try:
            self.driver.implicitly_wait(3)
            self.btn_wechat_share()
        except NoSuchElementException:
            self.btn_wechat_sent()
        try:
            self.driver.implicitly_wait(2)
            self.btn_wechat_back()
        except NoSuchElementException:
            pass
        time.sleep(2)

    def func_export_to_local(self):
        WebDriverWait(self.driver, 200).until(EC.presence_of_element_located((By.ID, self.HW_ENV + 'hor_iv1')))
        self.btn_local()
        try:
            self.driver.implicitly_wait(2)
            self.btn_sure()
        except NoSuchElementException:
            pass

    def func_export(self):
        self.func_export_to_wechat()